package com.yycx.module.bus.provider.service.hy.impl;

import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.common.mybatis.base.service.impl.BaseServiceImpl;
import com.yycx.common.mybatis.model.ResultBody;
import com.yycx.common.mybatis.query.CriteriaQuery;
import com.yycx.common.mybatis.query.CriteriaSave;
import com.yycx.common.mybatis.query.CriteriaUpdate;
import com.yycx.common.security.OpenHelper;
import com.yycx.common.utils.ApiAssert;
import com.yycx.module.bus.client.entity.hy.BusPolicyDrug;
import com.yycx.module.bus.client.entity.hy.BusPolicyPosition;
import com.yycx.module.bus.client.enums.DrugTypeEnum;
import com.yycx.module.bus.provider.mapper.hy.BusPolicyDrugMapper;
import com.yycx.module.bus.provider.service.BusDrugsService;
import com.yycx.module.bus.provider.service.hy.BusPolicyDrugService;
import com.yycx.module.bus.provider.service.hy.BusPolicyPositionService;
import com.yycx.module.system.client.entity.SysCompany;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 商品全局政策接口实现类
 *
 * @author flyme
 * @date 2023-07-16
 */
@Service
public class BusPolicyDrugServiceImpl extends BaseServiceImpl<BusPolicyDrugMapper, BusPolicyDrug> implements BusPolicyDrugService {

    @Autowired
    private BusPolicyPositionService policyPositionService;

    @Autowired
    @Lazy
    private BusDrugsService drugsService;

    @Override
    public ResultBody beforeAdd(CriteriaSave cs, BusPolicyDrug bpd, EntityMap extra) {
        bpd.setDrugState(1);
        String drugsName = bpd.getDrugsName();
        String drugsSpec = bpd.getDrugsSpec();
        Long companyId = OpenHelper.getCompanyId();
        bpd.setCompanyId(companyId);
        String drugType = bpd.getDrugType();
        if (DrugTypeEnum.ZS.getCode().equals(drugType)) {
            bpd.setDeptPrice(null);
            bpd.setDeptRate(null);
        }
        BusPolicyDrug busPolicyDrug = findPolicyDrugsName(drugsName, drugsSpec);
        ApiAssert.isEmpty("政策已存在,请勿重复添加", busPolicyDrug);
        return ResultBody.ok();
    }

    @Override
    public ResultBody afterAdd(CriteriaSave cs, BusPolicyDrug busPolicyDrug, EntityMap extra) {
        return super.afterAdd(cs, busPolicyDrug, extra);
    }


    @Override
    public ResultBody beforeEdit(CriteriaUpdate<BusPolicyDrug> cu, BusPolicyDrug bpd, EntityMap extra) {
        return super.beforeEdit(cu, bpd, extra);
    }

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public ResultBody beforePageList(CriteriaQuery<BusPolicyDrug> cq, BusPolicyDrug bpd, EntityMap requestMap) {
        cq.select(BusPolicyDrug.class, "*");
        cq.addSelect(SysCompany.class, "companyName");
        cq.like(BusPolicyDrug.class, "drugsName");
        cq.like(BusPolicyDrug.class, "drugCode");
        cq.eq(BusPolicyDrug.class, "drugType");
        cq.orderByDesc("bpd.createTime");
        cq.eq(BusPolicyDrug.class, "drugState", 1);
        cq.createJoin(SysCompany.class);
        return ResultBody.ok();
    }

    @Override
    public List<EntityMap> afterPageList(CriteriaQuery<BusPolicyDrug> cq, List<EntityMap> data, ResultBody resultBody) {
        for (EntityMap datum : data) {
            Long policyDrugId = datum.getLong("policyDrugId");
            List<BusPolicyPosition> list = policyPositionService.selectPolicyPositionList(policyDrugId);
            List<Long> drugIds = drugsService.getAuthList(policyDrugId);
            if (FlymeUtils.isNotEmpty(list)) {
                for (BusPolicyPosition policyPosition : list) {
                    Long positionId = policyPosition.getPositionId();
                    datum.put("priceType", policyPosition.getPriceType());
                    //大渠道商
                    if (positionId.equals(6L)) {
                        datum.put("settlePriceF2", policyPosition.getPositionPrice());
                        datum.put("settleRateF2", policyPosition.getPositionRate());
                    }
                    if (positionId.equals(7L)) {
                        datum.put("settlePrice", policyPosition.getPositionPrice());
                        datum.put("settleRate", policyPosition.getPositionRate());
                    }
                    //电商
                    if (positionId.equals(8L)) {
                        datum.put("settlePriceF3", policyPosition.getPositionPrice());
                        datum.put("settleRateF3", policyPosition.getPositionRate());
                    }
                    if (positionId.equals(5L)) {
                        datum.put("settlePriceD", policyPosition.getPositionPrice());
                        datum.put("settleRateD", policyPosition.getPositionRate());
                    }
                    if (positionId.equals(4L)) {
                        datum.put("settlePriceC", policyPosition.getPositionPrice());
                        datum.put("settleRateC", policyPosition.getPositionRate());
                    }
                    if (positionId.equals(2L)) {
                        datum.put("settlePriceE", policyPosition.getPositionPrice());
                        datum.put("settleRateE", policyPosition.getPositionRate());
                    }
                }
            }
            datum.put("policyPositionSize", drugIds.size());
        }
        return super.afterPageList(cq, data, resultBody);
    }

    @Override
    public BusPolicyDrug findPolicyDrugs(String drugCode, String drugsSpec, String producersName) {
        CriteriaQuery cq = new CriteriaQuery(BusPolicyDrug.class);
        cq.eq("drugCode", drugCode);
        cq.eq("drugsSpec", drugsSpec);
        cq.eq("producersName", producersName);
        cq.limit(1);
        return getOne(cq, false);
    }


    @Override
    public BusPolicyDrug findPolicyDrugsByName(String drugsName, String drugsSpec, String producersName) {
        CriteriaQuery cq = new CriteriaQuery(BusPolicyDrug.class);
        cq.eq("drugsName", drugsName);
        cq.eq("drugsSpec", drugsSpec);
        cq.eq("producersName", producersName);
        cq.limit(1);
        return getOne(cq, false);
    }

    @Override
    public BusPolicyDrug findPolicyDrugsName(String drugsName, String drugsSpec, Long companyId) {
        CriteriaQuery cq = new CriteriaQuery(BusPolicyDrug.class);
        cq.eq("drugsName", drugsName);
        cq.eq("drugsSpec", drugsSpec);
        cq.eq("companyId", companyId);
        cq.limit(1);
        return getOne(cq, false);
    }


    @Override
    public BusPolicyDrug findPolicyDrugsName(String drugsName, String drugsSpec) {
        BusPolicyDrug busPolicyDrug = null;
        CriteriaQuery cq = new CriteriaQuery(BusPolicyDrug.class);
        cq.likeRight("drugsName", drugsName);
        cq.likeRight("drugsSpec", drugsSpec);
        List<BusPolicyDrug> list = list(cq);
        if (FlymeUtils.isNotEmpty(list) && list.size() > 1) {
            Long companyId = OpenHelper.getCompanyId();
            busPolicyDrug = findPolicyDrugsName(drugsName, drugsSpec, companyId);
        }
        if (FlymeUtils.isNotEmpty(list) && list.size() == 1) {
            busPolicyDrug = list.get(0);
        }
        return busPolicyDrug;
    }
}
